const app = getApp()
import {
  getOrderList,
  cancelOrder,
  payOrder,
  delOrder,
  sureOrder,
} from '../../api/order'
import {
  wxPay,
} from '../../api/pay'
let currentPage = 1
let showCount = 10
let totalPage = 1
let loading = false;
const statusMap = {
  '0': '待付款',
  '1': '待发货',
  '2': '待收货',
  '3': '已完成',
  '4': '已取消',
}
Page({
  data: {
    currentOrderId:'',
    expressVisible: false,
    currentStatus: '',
    tab: [

      {
        id: 1,
        name: '全部',
        status: '',
      },
      {
        id: 2,
        name: '待付款',
        status: '0',
      },
      {
        id: 3,
        name: '待收货',
        status: '1',
      },
      {
        id: 4,
        name: '已完成',
        status: '3',
      },
      {
        id: 5,
        name: '已取消',
        status: '4',
      },
    ],
    list: [],
    shopName: wx.getStorageSync('shop_name'),
    postInfo: {
      postAddress: '',
      postPhone: '',
      postReceiver: '',
    }
  },
  onClickLeft() {
    // wx.showToast({
    //   title: '点击返回',
    //   icon: 'none'
    // });
    wx.navigateBack({
      delta: 1,
    })
  },
  onShow() {
    this.initList()
  },
  onLoad(options) {
    if (options.status) {
      this.setData({
        currentStatus: options.status
      })
    }
  },
  onReachBottom() {
    if (currentPage >= totalPage) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return
    }
    currentPage++
    this.fetchList()
  },
  onChange(e) {

    const name = e.detail.name
    this.setData({
      currentStatus: String(name)
    })
    this.initList()
  },
  //#region  方法
  initList() {
    this.setData({
      list: []
    })
    totalPage = 1
    currentPage = 1
    this.fetchList()
  },
  fetchList() {
    if(loading){
      return ;
    }
    loading =true
    const {
      currentStatus: status
    } = this.data
    const params = {
      currentPage,
      showCount,
    }
    if (Number(status) > -1) {
      params.status = status
    }

    getOrderList(params).then(res => {
      // console.log(res)
      loading =false
      const {
        list,
        totalPage: total
      } = res.data
      if (total) {
        totalPage = total
      }
      // console.log( list )
      list.forEach((item) => {
        item.status = String(item.status)
        // console.log( item )
        item.statusName = statusMap[item.status]
        if (item.pic) {
          item.picUrl = item.pic.split(',')[0]
        } else {
          item.picUrl = ''
        }
        if(item.items){
          item.items = item.items.map( item => {
            let pic = item.productPic.split(',')
            let productPic = item.productPic
            if(pic.length > 1){
              productPic = pic[0]
            }
            return {
              ...item,
              productPic: productPic
            }
          })
        }

      })
      // console.log(list)
      this.setData({
        list: this.data.list.concat(list)
      })
    })
    .catch( () =>{
      loading =false

    })
  },
  ////#endregion 
  cancelOrder(e) {
    const {
      id
    } = e.currentTarget.dataset
    // this.setData({
    //   currentStatus: '4'
    // })
    // return
    cancelOrder({
        orderId: String(id)
      }).then(res => {
        wx.showToast({
          title: '取消成功',
          icon: 'none'
        })
        // console.log(res)
        this.setData({
          currentStatus: '4'
        })
        // this.initList();
      })
      .catch(err => {
        console.log(err)
      })

  },
  payOrder(e) {
    const {
      id
    } = e.currentTarget.dataset
    payOrder({
        orderId: String(id)
      }).then(res => {

        // console.log(res)
        

        return wxPay(res.data.json)
        // this.initList();
      })
      .then(res => {
        this.setData({
          currentStatus: '1'
        })
        wx.showToast({
          title: '付款成功',
          icon: 'none'
        })
      })
      .catch(err => {
        console.log(err)
      })
  },
  sureExpressd(e){
    const {id} = e.currentTarget.dataset
    wx.showModal({
      title: '确定收货吗？ 请在确认签收后确定',
      
      cancelColor: 'cancelColor',
      success: (e) => {
        // console.log(e)
        if(e.confirm){
          this.fetchSureOrder(id)
        }
      }
    })
  },
  delOrder(e){
 
    const {id} = e.currentTarget.dataset
    wx.showModal({
      title: '确定删除吗？',
      cancelColor: 'cancelColor',
      success: (e) => {
        // console.log(e)
        if(e.confirm){
          this.fetchDelOrder(id)
        }
      }
    })
  },
  seeExpress(e){
    const {id , index} = e.currentTarget.dataset
    // wx.showToast({
    //   title: '查看物流信息' + id,
    //   icon: 'none'
    // })
    let { postAddress ,postPhone, postReceiver,items } = this.data.list[index]

    let goodImg = items[0].productPic
    console.log('goodImg',goodImg)
    this.setData({
      expressVisible: true,
      currentOrderId: id,
      goodImg:goodImg,
      postInfo: {
        postAddress,
        postPhone,
        postReceiver,
      }
    })

  },
  closeExpress(e){
    this.setData({
      expressVisible: false,
      currentOrderId: '',
      postInfo: null,
    })
  },
  fetchDelOrder(id){
    // console.log(id)
    delOrder({
      orderId: String(id)
    })
    .then(res => {
      // console.log(res)
      wx.showToast({
        title: '删除成功',
        icon: 'none'
      })
      this.initList()
    })
    .catch(err => {
      wx.showToast({
        title: '删除失败',
        icon: 'none'
      })
      console.log(err)
    })
  },
  fetchSureOrder(id){
    // console.log(id)
    sureOrder({
      orderId: String(id)
    })
    .then(res => {
      // console.log(res)
      wx.showToast({
        title: '收货成功',
        icon: 'none'
      })
      this.setData({
        currentStatus: '3'
      })
      this.initList()
    })
    .catch(err => {
      wx.showToast({
        title: '收货失败',
        icon: 'none'
      })
      console.log(err)
    })
  },

  toOrderDetail(e){
    // console.log(e)
    const { id } =  e.currentTarget.dataset
    // console.log(id)
    wx.navigateTo({
      url: `/pages/order/detail/detail?orderId=${id}`,
    })
  }
})